From 6b89d8a199db9fe918f055fe5006e8bc20cb8d8a Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 27 Dec 2019 12:16:14 -0500 Subject: [PATCH] inspector: Add a focus overlay It helps to see what is going on. --- gtk/inspector/focusoverlay.c | 111 +++++++++++++++++++++++++++++++++++ gtk/inspector/focusoverlay.h | 36 ++++++++++++ gtk/inspector/meson.build | 1 + gtk/inspector/visual.c | 47 +++++++++++++++ gtk/inspector/visual.ui | 27 +++++++++ 5 files changed, 222 insertions(+) create mode 100644 gtk/inspector/focusoverlay.c create mode 100644 gtk/inspector/focusoverlay.h diff --git a/gtk/inspector/focusoverlay.c b/gtk/inspector/focusoverlay.c new file mode 100644 index 0000000000..1ed260d077 --- /dev/null +++ b/gtk/inspector/focusoverlay.c @@ -0,0 +1,111 @@ +/* + * Copyright © 2018 Benjamin Otte + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + * Authors: Benjamin Otte + */ + +#include "config.h" + +#include "focusoverlay.h" + +#include "gtkintl.h" +#include "gtkwidget.h" +#include "gtkroot.h" +#include "gtknative.h" + +struct _GtkFocusOverlay +{ + GtkInspectorOverlay parent_instance; + + GdkRGBA color; +}; + +struct _GtkFocusOverlayClass +{ + GtkInspectorOverlayClass parent_class; +}; + +G_DEFINE_TYPE (GtkFocusOverlay, gtk_focus_overlay, GTK_TYPE_INSPECTOR_OVERLAY) + +static void +gtk_focus_overlay_snapshot (GtkInspectorOverlay *overlay, + GtkSnapshot *snapshot, + GskRenderNode *node, + GtkWidget *widget) +{ + GtkFocusOverlay *self = GTK_FOCUS_OVERLAY (overlay); + GtkWidget *focus; + graphene_rect_t bounds; + + if (!GTK_IS_NATIVE (widget)) + return; + + focus = gtk_root_get_focus (GTK_ROOT (gtk_widget_get_root (widget))); + if (!focus) + return; + + if (!gtk_widget_is_ancestor (focus, widget)) + return; + + if (GTK_WIDGET (gtk_widget_get_native (focus)) != widget) + return; + + if (!gtk_widget_compute_bounds (focus, widget, &bounds)) + return; + + gtk_snapshot_append_color (snapshot, &self->color, &bounds); +} + +static void +gtk_focus_overlay_queue_draw (GtkInspectorOverlay *overlay) +{ +} + +static void +gtk_focus_overlay_class_init (GtkFocusOverlayClass *klass) +{ + GtkInspectorOverlayClass *overlay_class = GTK_INSPECTOR_OVERLAY_CLASS (klass); + + overlay_class->snapshot = gtk_focus_overlay_snapshot; + overlay_class->queue_draw = gtk_focus_overlay_queue_draw; +} + +static void +gtk_focus_overlay_init (GtkFocusOverlay *self) +{ + self->color = (GdkRGBA) { 0.5, 0.0, 1.0, 0.2 }; +} + +GtkInspectorOverlay * +gtk_focus_overlay_new (void) +{ + GtkFocusOverlay *self; + + self = g_object_new (GTK_TYPE_FOCUS_OVERLAY, NULL); + + return GTK_INSPECTOR_OVERLAY (self); +} + +void +gtk_focus_overlay_set_color (GtkFocusOverlay *self, + const GdkRGBA *color) +{ + if (gdk_rgba_equal (&self->color, color)) + return; + + self->color = *color; + gtk_inspector_overlay_queue_draw (GTK_INSPECTOR_OVERLAY (self)); +} diff --git a/gtk/inspector/focusoverlay.h b/gtk/inspector/focusoverlay.h new file mode 100644 index 0000000000..c2acd99947 --- /dev/null +++ b/gtk/inspector/focusoverlay.h @@ -0,0 +1,36 @@ +/* + * Copyright © 2018 Benjamin Otte + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + * Authors: Benjamin Otte + */ + +#ifndef __GTK_FOCUS_OVERLAY_H__ +#define __GTK_FOCUS_OVERLAY_H__ + +#include "inspectoroverlay.h" + +G_BEGIN_DECLS + +#define GTK_TYPE_FOCUS_OVERLAY (gtk_focus_overlay_get_type ()) +G_DECLARE_FINAL_TYPE (GtkFocusOverlay, gtk_focus_overlay, GTK, FOCUS_OVERLAY, GtkInspectorOverlay) + +GtkInspectorOverlay * gtk_focus_overlay_new (void); +void gtk_focus_overlay_set_color (GtkFocusOverlay *self, + const GdkRGBA *color); + +G_END_DECLS + +#endif /* __GTK_FOCUS_OVERLAY_H__ */ diff --git a/gtk/inspector/meson.build b/gtk/inspector/meson.build index a26d67ed87..dbcea03bb0 100644 --- a/gtk/inspector/meson.build +++ b/gtk/inspector/meson.build @@ -6,6 +6,7 @@ inspector_sources = files( 'css-editor.c', 'css-node-tree.c', 'data-list.c', + 'focusoverlay.c', 'fpsoverlay.c', 'general.c', 'graphdata.c', diff --git a/gtk/inspector/visual.c b/gtk/inspector/visual.c index 183b30cf8c..5787f12756 100644 --- a/gtk/inspector/visual.c +++ b/gtk/inspector/visual.c @@ -23,6 +23,7 @@ #include "fpsoverlay.h" #include "updatesoverlay.h" #include "layoutoverlay.h" +#include "focusoverlay.h" #include "window.h" #include "gtkadjustment.h" @@ -81,6 +82,7 @@ struct _GtkInspectorVisualPrivate GtkWidget *baselines_switch; GtkWidget *layout_switch; GtkWidget *resize_switch; + GtkWidget *focus_switch; GtkWidget *misc_box; GtkWidget *touchscreen_switch; @@ -91,6 +93,7 @@ struct _GtkInspectorVisualPrivate GtkInspectorOverlay *fps_overlay; GtkInspectorOverlay *updates_overlay; GtkInspectorOverlay *layout_overlay; + GtkInspectorOverlay *focus_overlay; GdkDisplay *display; }; @@ -414,6 +417,41 @@ widget_resize_activate (GtkSwitch *sw) gtk_set_debug_flags (flags); } +static void +focus_activate (GtkSwitch *sw, + GParamSpec *pspec, + GtkInspectorVisual *vis) +{ + GtkInspectorVisualPrivate *priv = vis->priv; + GtkInspectorWindow *iw; + gboolean focus; + + focus = gtk_switch_get_active (sw); + iw = GTK_INSPECTOR_WINDOW (gtk_widget_get_root (GTK_WIDGET (vis))); + if (iw == NULL) + return; + + if (focus) + { + if (priv->focus_overlay == NULL) + { + priv->focus_overlay = gtk_focus_overlay_new (); + gtk_inspector_window_add_overlay (iw, priv->focus_overlay); + g_object_unref (priv->focus_overlay); + } + } + else + { + if (priv->focus_overlay != NULL) + { + gtk_inspector_window_remove_overlay (iw, priv->focus_overlay); + priv->focus_overlay = NULL; + } + } + + redraw_everything (); +} + static void fill_gtk (const gchar *path, GHashTable *t) @@ -939,6 +977,11 @@ row_activated (GtkListBox *box, GtkSwitch *sw = GTK_SWITCH (vis->priv->resize_switch); gtk_switch_set_active (sw, !gtk_switch_get_active (sw)); } + else if (gtk_widget_is_ancestor (vis->priv->focus_switch, GTK_WIDGET (row))) + { + GtkSwitch *sw = GTK_SWITCH (vis->priv->focus_switch); + gtk_switch_set_active (sw, !gtk_switch_get_active (sw)); + } else if (gtk_widget_is_ancestor (vis->priv->touchscreen_switch, GTK_WIDGET (row))) { GtkSwitch *sw = GTK_SWITCH (vis->priv->touchscreen_switch); @@ -1028,6 +1071,8 @@ gtk_inspector_visual_finalize (GObject *object) gtk_inspector_window_remove_overlay (iw, vis->priv->updates_overlay); if (vis->priv->fps_overlay) gtk_inspector_window_remove_overlay (iw, vis->priv->fps_overlay); + if (vis->priv->focus_overlay) + gtk_inspector_window_remove_overlay (iw, vis->priv->focus_overlay); G_OBJECT_CLASS (gtk_inspector_visual_parent_class)->finalize (object); } @@ -1104,6 +1149,7 @@ gtk_inspector_visual_class_init (GtkInspectorVisualClass *klass) gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorVisual, baselines_switch); gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorVisual, layout_switch); gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorVisual, resize_switch); + gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorVisual, focus_switch); gtk_widget_class_bind_template_callback (widget_class, fps_activate); gtk_widget_class_bind_template_callback (widget_class, updates_activate); @@ -1112,6 +1158,7 @@ gtk_inspector_visual_class_init (GtkInspectorVisualClass *klass) gtk_widget_class_bind_template_callback (widget_class, baselines_activate); gtk_widget_class_bind_template_callback (widget_class, layout_activate); gtk_widget_class_bind_template_callback (widget_class, widget_resize_activate); + gtk_widget_class_bind_template_callback (widget_class, focus_activate); gtk_widget_class_bind_template_callback (widget_class, software_gl_activate); } diff --git a/gtk/inspector/visual.ui b/gtk/inspector/visual.ui index ead50b6384..d052c9d68c 100644 --- a/gtk/inspector/visual.ui +++ b/gtk/inspector/visual.ui @@ -529,6 +529,32 @@ + + + + + 10 + 40 + + + Show Focus + start + baseline + 0.0 + + + + + end + baseline + 1 + + + + + + + @@ -616,6 +642,7 @@ + -- 2.30.2